import 'dart:convert';

class Sample {
  String username;
  int favouriteInteger;
  double favouriteDouble;
  String url;
  String htmlUrl;
  List<String> tags;
  List<int> randomIntegers;
  List<double> randomDoubles;
  PersonalInfo personalInfo;

  Sample({
    this.username,
    this.favouriteInteger,
    this.favouriteDouble,
    this.url,
    this.htmlUrl,
    this.tags,
    this.randomIntegers,
    this.randomDoubles,
    this.personalInfo,
  });

  Sample.fromJson(Map<String, dynamic> jsonMap) {
    username = jsonMap['username'];
    favouriteInteger = jsonMap['favouriteInteger'];
    favouriteDouble = jsonMap['favouriteDouble'];
    url = jsonMap['url'];
    htmlUrl = jsonMap['html_url'];
    tags = (jsonMap['tags'] is String
            ? json.decode(jsonMap['tags'])
            : jsonMap['tags'])
        .cast<String>();
    randomIntegers = (jsonMap['randomIntegers'] is String
            ? json.decode(jsonMap['randomIntegers'])
            : jsonMap['randomIntegers'])
        .cast<int>();
    randomDoubles = (jsonMap['randomDoubles'] is String
            ? json.decode(jsonMap['randomDoubles'])
            : jsonMap['randomDoubles'])
        .cast<double>();
    if (jsonMap['personalInfo'] != null) {
      var _dataMap = jsonMap['personalInfo'] is String
          ? json.decode(jsonMap['personalInfo'])
          : jsonMap['personalInfo'];
      personalInfo = new PersonalInfo.fromJson(_dataMap);
    }
  }

  Map<String, dynamic> toJson([bool isSql = false]) {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['username'] = this.username;
    data['favouriteInteger'] = this.favouriteInteger;
    data['favouriteDouble'] = this.favouriteDouble;
    data['url'] = this.url;
    data['html_url'] = this.htmlUrl;
    data['tags'] = isSql ? json.encode(this.tags) : this.tags;
    data['randomIntegers'] =
        isSql ? json.encode(this.randomIntegers) : this.randomIntegers;
    data['randomDoubles'] =
        isSql ? json.encode(this.randomDoubles) : this.randomDoubles;
    if (this.personalInfo != null) {
      if (isSql) {
        data['personalInfo'] = json.encode(this.personalInfo);
      } else {
        data['personalInfo'] = this.personalInfo.toJson();
      }
    }
    return data;
  }
}

class PersonalInfo {
  String firstName;
  String lastName;
  String location;
  List<Phones> phones;

  PersonalInfo({
    this.firstName,
    this.lastName,
    this.location,
    this.phones,
  });

  PersonalInfo.fromJson(Map<String, dynamic> jsonMap) {
    firstName = jsonMap['firstName'];
    lastName = jsonMap['lastName'];
    location = jsonMap['location'];
    if (jsonMap['phones'] != null) {
      phones = [];
      var _list = (jsonMap['phones'] is String
          ? json.decode(jsonMap['phones'])
          : jsonMap['phones']);
      _list.forEach((v) {
        phones.add(new Phones.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson([bool isSql = false]) {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['firstName'] = this.firstName;
    data['lastName'] = this.lastName;
    data['location'] = this.location;
    if (this.phones != null) {
      if (isSql) {
        data['phones'] = json.encode(this.phones);
      } else {
        data['phones'] = this.phones.map((v) => v.toJson()).toList();
      }
    }
    return data;
  }
}

class Phones {
  String type;
  String number;
  bool shouldCall;

  Phones({
    this.type,
    this.number,
    this.shouldCall,
  });

  Phones.fromJson(Map<String, dynamic> jsonMap) {
    type = jsonMap['type'];
    number = jsonMap['number'];
    shouldCall = jsonMap['shouldCall'] is int
        ? jsonMap['shouldCall'] == 1
        : jsonMap['shouldCall'];
  }

  Map<String, dynamic> toJson([bool isSql = false]) {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['type'] = this.type;
    data['number'] = this.number;
    if (isSql) {
      data['shouldCall'] = this.shouldCall ? 1 : 2;
    } else {
      data['shouldCall'] = this.shouldCall;
    }
    return data;
  }
}
